import { stringify } from 'qs';
import request from '@utils/request';

export default {
  query({ name, type, url, description, page = 0, size = 10 }) {
    return request(`/api/cms/material?${stringify({ name, type, url, description, page, size })}`);
  },

  info({ id }) {
    return request(`/api/cms/material/${id}`);
  },

  add({ name, file, description }) {
    const data = new FormData();
    data.append('name', name);
    data.append('description', description);
    data.append('file', file);
    return request('/api/cms/material', {
      method: 'POST',
      mode: 'cors',
      body: data,
    });
  },

  update({ id, name, type, url, description }) {
    return request(`/api/cms/material/${id}`, {
      method: 'PUT',
      body: {
        id,
        name,
        type,
        url,
        description,
      },
    });
  },

  remove({ ids }) {
    const result = Promise.all(
      ids.map(id => {
        return request(`/api/cms/material/${id}`, {
          method: 'DELETE',
        });
      })
    );
    return result
      .then(() => {
        return '删除成功';
      })
      .catch(e => {
        return e.message || '删除失败';
      });
  },
};
